

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.search.SearchHits;
import org.nlpcn.es4sql.exception.SqlParseException;
import org.nlpcn.es4sql.query.ESActionFactory;
import org.nlpcn.es4sql.query.QueryAction;

public class TestSql {
	public TransportClient init(){
		TransportClient client = null;
			Settings settings = Settings.settingsBuilder()
					.put("cluster.name", "dmp_es_cluster").build();
			try {
				client = TransportClient.builder().settings(settings).build()
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.9"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.10"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.11"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.12"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.13"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.14"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.15"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.16"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.17"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.18"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.19"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.20"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.21"), 9300))
						.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.8.1.22"), 9300));
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return client;
	}
	public static void main(String[] args) throws SQLFeatureNotSupportedException, SqlParseException, IOException {
		TestSql ts = new TestSql();
		Client cl = ts.init();
		long beginTime = System.currentTimeMillis();
		//SearchDao searchDao = new SearchDao(ts.client);
		//String sql = "SELECT c1,c2,c3 count FROM es_data/test limit 10";
		String sql = "SELECT * FROM myindex/test limit 1000000";
		QueryAction qa = ESActionFactory.create(cl, sql);
		SearchResponse  sr = (SearchResponse)qa.explain().get();
		//System.out.println(sr);
		//System.out.println(sr.getHits().getHits()[0].getSource().get("count"));
		// 获得返回结果
		SearchHits hits = sr.getHits();
		System.out.println("总数据量为:"+hits.getTotalHits());
		System.out.println("第一列 \t\t第二列\t\t第三列");
		 File file = new File("E:\\b.txt");// 指定要写入的文件  
	        if (!file.exists()) {// 如果文件不存在则创建  
	            file.createNewFile();  
	        }  
        // 获取该文件的缓冲输出流  
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));  
		for (int i = 0; i < hits.getHits().length; i++) {
			 Map map = hits.getHits()[i].getSource();
			 System.out.println(map.get("user_id")+"\t\t"+ map.get("type_id")+"\t\t"+map.get("pv"));
		        // 写入信息  
		        bufferedWriter.write(map.get("user_id")+"\t\t"+ map.get("type_id")+"\t\t"+map.get("pv"));  
		        bufferedWriter.newLine();// 表示换行  
		       
		}
		 bufferedWriter.flush();// 清空缓冲区  
	     bufferedWriter.close();// 关闭输出流  
		System.out.println("耗时(毫秒):"+(System.currentTimeMillis()-beginTime));
       /* SqlElasticRequestBuilder actionRequestBuilder = searchDao.explain(sql);
		String jsonExplanation = actionRequestBuilder.explain();
        System.out.println(actionRequestBuilder.getBuilder().request().getContext().get("c1"));*/
	}
}
